<template>
    <div :class="className" :style="{height:height,width:width}" />
  </template>
  
  <script>
//   import echarts from 'echarts'
  import * as echarts from 'echarts';
 
  import resize from './mixins/resize'
  const animationDuration = 600
  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      },
      Chartdata:{
        type:Array ,
        required: true
      },
      theme: {
        type: String,
        default: ''
      },
    },
    data() {
      return {
        chart: null,
        color:"#000",
      }
    },
    watch:{
      Chartdata:{
        deep:true,
        handler(val){
          this.setOptions(val)
          if(val=="macarons" || val=="jazz"){
            this.color="#fff"
          }
        }
      } ,
      theme:{
        deep:true,
        handler(val){
          if(val=="macarons" || val=="jazz"){
            this.color="#fff"
          }
        }
      } 
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        require('echarts/theme/'+this.theme+'') // echarts theme
        this.chart = echarts.init(this.$el, ''+this.theme+'')
        if(this.theme=="macarons" || this.theme=="jazz"){
            this.color="#fff"
          }
        this.setOptions(this.Chartdata)
      },
      setOptions(data) {
        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            },
            confine:true,
            textStyle:{
              color:this.color,
            }
          },
          title: {
          text: '特性示例：渐变色 阴影 点击缩放',
          left:"center",
          textStyle:{
            color:'#000',
            fontWeight:'normal',
            fontSize:"16px"
          }
        },
          grid: {
            top: 50,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            }
          }],
          series: [{
            name: 'pageA',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: data[0],
            animationDuration
          }, {
            name: 'pageB',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: data[1],
            animationDuration
          }, {
            name: 'pageC',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: data[2],
            animationDuration
          }]
        })
      }

    }
  }
  </script>